package com.zyw.customcalendar.view;

import java.util.Calendar;

import com.zyw.customcalendar.event.EventIndex;
import com.zyw.customcalendar.operation.CalendarDraw;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import de.greenrobot.event.EventBus;

@SuppressLint({ "DrawAllocation", "ClickableViewAccessibility" })
public class CalendarView extends View {

	private CalendarDraw calendarDraw;

	public CalendarView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		calendarDraw = new CalendarDraw();
	}

	public CalendarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		calendarDraw = new CalendarDraw();
	}

	public CalendarView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		calendarDraw = new CalendarDraw();
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		if(calendarDraw == null){
			calendarDraw = new CalendarDraw();
			calendarDraw.Draw(canvas);
		}else{
			calendarDraw.Draw(canvas);
		}
		super.onDraw(canvas);
	}



	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction() == MotionEvent.ACTION_UP){
			if(calendarDraw != null) {
				DayClick(event.getX(), event.getY());
			}
		}
		return true;
	}

	private void DayClick(float ex,float ey){
		calendarDraw.ex = ex;
		calendarDraw.ey = ey;
		int k = -1;
		if(calendarDraw != null && (k = calendarDraw.setRdRect()) != -1){
			calendarDraw.isRDraw = false;
			calendarDraw.post(k);
			postInvalidate((int) calendarDraw.rdRect.left, (int) calendarDraw.rdRect.top,
					(int) calendarDraw.rdRect.right, (int)calendarDraw.rdRect.bottom);
		}
	}

	public void autoClickDays(int selectDayOfMonth) {
		int k = -1;
		if(calendarDraw != null && (k = calendarDraw.setRdRect(selectDayOfMonth)) != -1){
			calendarDraw.isRDraw = false;
			calendarDraw.post(k);
			postInvalidate((int) calendarDraw.rdRect.left, (int) calendarDraw.rdRect.top,
					(int) calendarDraw.rdRect.right, (int)calendarDraw.rdRect.bottom);
		}
	}

	public int cHeight = 0;
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		if(calendarDraw != null && heightSize > 0){
			heightSize = (int) calendarDraw.setAllHeight(widthSize);
		}
		cHeight = heightSize;
		calendarDraw.initData(widthSize);
		setMeasuredDimension(widthSize, heightSize);
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
	}

	public CalendarDraw getCalendarDraw() {
		return calendarDraw;
	}

	public void setCalendarDraw(CalendarDraw calendarDraw) {
		this.calendarDraw = calendarDraw;
	}

	public void setNextCalendar(Calendar calendar){
		calendarDraw.setNextCalendar(calendar);
		this.requestLayout();
		this.invalidate();
	}

	public void setPreviousCalendar(Calendar calendar){
		calendarDraw.setPreviousCalendar(calendar);
		this.requestLayout();
		this.invalidate();
	}

	public void refreshCalendar() {
		// TODO Auto-generated method stub
		calendarDraw.refreshCalendar();
		this.invalidate();
	}
}
